package org.hackystat.sdsa.analyzer.control.tokenizer;

import java.util.List;

import org.hackystat.sdsa.analyzer.model.activity.Activity;
import org.hackystat.sdsa.analyzer.model.activity.UnitTestActivity;

/**
 * Implements the test pass tokenizer to divide a development stream into test 
 * pass episodes.
 * 
 * @author hongbing
 */
public class UnitTestPassTokenizer extends Tokenizer {
  /**
   * Looks ahead for next test pass.
   * 
   * @param activityList Development activity list.
   * @param start Place to start new episode.
   * @return Index of the next test pass activity.
   */
  @Override public int lookAhead(List<Activity> activityList, int start) {
    int cursor = -1;    
    for (cursor = start; cursor < activityList.size(); cursor++) {
      if (isTestPassActivity(activityList.get(cursor))) {
        break;
      }
    }
    
    if (cursor < activityList.size()) {
      return cursor;
    }
    else {
      return activityList.size() - 1;
    }
  }
  
  /**
   * Checks whether an activity is test pass.
   * 
   * @param activity Activity.
   * @return True if it is a test and it is successful.
   */
  private boolean isTestPassActivity(Activity activity) {
    if (activity instanceof UnitTestActivity) {
      UnitTestActivity unitTestActivity = (UnitTestActivity) activity;
      // Stop when there is test pass
      return unitTestActivity.isSuccessful();
    }
    else {
      return false;  
    }    
  }
}
